import sortedcontainers
class Solution(object):
    def countSmaller(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        stack = sortedcontainers.SortedList()
        nums.reverse()
        n = len(nums)
        out = [0] * n
        for i in range(n):
            index = stack.bisect_left(nums[i])
            stack.add(nums[i])
            out[i] = index
        out.reverse()
        return out

data = Solution()
nums = [5, 2, 6, 1]
print(data.countSmaller(nums))
